package com.aaa.sso.service.impl;

import com.aaa.common.bo.Member;
import com.aaa.common.util.Result;
import com.aaa.sso.service.AuthService;
import com.aaa.sso.util.CustomUsernamePasswordToken;
import com.aaa.common.util.JWTUtils;
import com.aaa.sso.util.LoginType;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Xiujie Yu
 * @Date 2021-01-21
 * @Time 14:47
 */
@Service("authMemberService")
public class AuthMemberServiceImpl implements AuthService {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public Result<Map<String,Object>> authUserMember(String userName,String password){

        Map<String, Object> resultMap = new HashMap<>();
        //收集用户信息
        CustomUsernamePasswordToken usernamePasswordToken = new CustomUsernamePasswordToken(userName, password, LoginType.MEMBER.toString());
        //获取用户主体 subject 当前登陆用户（shiro）
        Subject subject = SecurityUtils.getSubject();
        //实例化返回前端的map
        Result<Map<String,Object>> result = new Result<>();
        try {
            subject.login(usernamePasswordToken);
            Member member = (Member) subject.getPrincipal();
            //生成jwt
            HashMap<String, String> map = new HashMap<>();
            map.put("id",member.getId()+"");
            map.put("name",member.getName());
            map.put("username",member.getUsername());
            map.put("accountBalance",member.getAccountBalance()+"");
            map.put("integration",member.getIntegration()+"");
            map.put("type","member");
            String token = JWTUtils.initToken(15, map);
            //给前端返回的map
            resultMap.put("loginSuccess",token);
            resultMap.put("id",member.getId()+"");
            resultMap.put("name",member.getName());
            resultMap.put("username",member.getUsername());
            resultMap.put("headPic",member.getHeadPic());
            resultMap.put("phoneNum",member.getPhoneNum());
            resultMap.put("accountBalance",member.getAccountBalance()+"");
            resultMap.put("integration",member.getIntegration()+"");
            resultMap.put("idType",member.getIdType());
            resultMap.put("licenseNo",member.getLicenseNo());
            resultMap.put("idNumber",member.getIdCard());
            resultMap.put("email",member.getEmail());
            result.setResultMessage("认证通过");
            result.setResultCode(200);
            redisTemplate.opsForValue().set(token,member,15,TimeUnit.DAYS);
        }catch (AuthenticationException e) {
            e.printStackTrace();
            result.setResultMessage("用户名或密码错误");
            result.setResultCode(500);
            resultMap.put("loginFail", "用户名或密码错误");
        }
        result.setT(resultMap);
        return result;
    }

    @Override
    public boolean checkToken(String token){
       return JWTUtils.verifyToken(token);
    }

    @Override
    public boolean updateUserPassword(String username, String oldPassword, String newPassword) {
        return false;
    }
}
